package com.example.tnot.Teacher;



import static com.example.tnot.login.fragmentui.LoginFragment.BASE_URL;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.bumptech.glide.Glide;
import com.example.tnot.R;
import com.example.tnot.Student.DebugStudentActivity;
import com.example.tnot.Student.DebugVideoActivity;
import com.example.tnot.api.HttpUtils;
import com.example.tnot.api.SocketClient;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class DebugTeacherActivity extends AppCompatActivity {

    private Button give_permission_button;
    private Button get_avatar;
    private Button to_live;
    private ImageView gifImageView;
    private String GET_AVATAR_URL = BASE_URL+"/api/get_user_avatar/";

    private EditText avatar_id ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_debug_teacher);
        give_permission_button = findViewById(R.id.button2);
        avatar_id = findViewById(R.id.editTextText);
        get_avatar = findViewById(R.id.get_avatar);
        gifImageView = findViewById(R.id.gif_image_view);
        to_live = findViewById(R.id.to_live);
         //连接上teacher tcp
        SocketClient socketClient = SocketClient.getInstance();

        // 先建立连接，再调用teacherConnect（与TeacherLiveActivity保持一致）
        socketClient.connect();
        while(!socketClient.getmSocket())
        {;}
         socketClient.teacherConnect();


         //给予权限
         give_permission_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                JSONObject data =  new JSONObject();
                    try {
                        data.put("messge","teacher");
                    } catch (JSONException e) {
                        throw new RuntimeException(e);
                    }
                    socketClient.sendEmit("change_permission",data);
                }
         });

         get_avatar.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {

                 String id = avatar_id.getText().toString();
                 String URL = GET_AVATAR_URL+id;
                 change_avatar(URL);


             }
         });

         to_live.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                 Intent it =new Intent(DebugTeacherActivity.this, TeacherLiveActivity.class);
                 startActivity(it);
             }
         });

    }
    private String getTimestampUrl(String originalUrl) {
        return originalUrl;// + "?v=" + System.currentTimeMillis();
    }
    private void change_avatar(String URL)
    {
        Glide.with(this)
                .load(getTimestampUrl(URL)) // 传入从后端获取的 GIF URL
                .into(gifImageView); // 显示到 ImageView
    }

}